<?php
require_once(SITE_ROOT."/includes/ShowPage.php");
class RecordSet {
    public $recordArray;
    public function __construct() {
        $this->recordArray = array();
    }
}

class PagedRecordSet extends RecordSet {
    public $offset;
    public $totalcount;
    public $pagesize;
    
    public function __construct($offset, $totalcount, $pagesize) {
        parent::__construct();
        $this->offset = $offset;
        $this->totalcount = $totalcount;
        $this->pagesize = $pagesize;
    }
    public function getPaginationHTML($pagevarname) {
        $page = new ShowPage($this->totalcount, $this->pagesize, $pagevarname);
        return $page->page();
    }
    
    public function getPaginationAjaxHTML($pagevarname,$function_name="showPage") {
        $page = new ShowPage($this->totalcount, $this->pagesize, $pagevarname);
        return array($page->ajaxPage($function_name),$page->getCurrentPageNo());
    }
}

class PrevNextRecordSet extends RecordSet {
    public $offset;
    public $pagesize;
    
    public function __construct($offset, $pagesize) {
        parent::__construct();
        $this->offset = $offset;
        $this->pagesize = $pagesize;
    }
    public function hasPreviousPage() {
        return ($this->offset > 0);
    }
    
    public function hasNextPage() {
        return (count($this->recordArray) >= $this->pagesize);
    }
    
    public function getPaginationHTML($pagevarname) {
        if (hasPreviousPage()) {
            $ret .= '<a href="?';
            foreach($_GET as $k => $v) {
                $ret .= ($k<>$pagevarname) ? $k."=".urlencode($v)."&" : "";
            }
            $ret .= $pagevarname.'='.($_GET[$pagevarname] ? intval($_GET[$pagevarname])-1 : 1);
            $ret .= '">上一页</a>';
        } else {
            $ret .= '<span style="color:#aaaaaa">上一页</span>';
        }
        $ret .= ' - ';
        if (hasNextPage()) {
            $ret .= '<a href="?';
            foreach($_GET as $k => $v) {
                $ret .= ($k<>$pagevarname) ? $k."=".urlencode($v)."&" : "";
            }
            $ret .= $pagevarname.'='.($_GET[$pagevarname] ? intval($_GET[$pagevarname])+1 : 2);
            $ret .= '">下一页</a>';
        } else {
            $ret .= '<span style="color:#aaaaaa">下一页</span>';
        }
        return $ret;
    }
    
    public function getPaginationAjaxHTML($pagevarname,$function_name="showPage") {
        $phpSelf=str_replace("modules","ajax",$_SERVER['PHP_SELF']);
        if ($this->hasPreviousPage()) {
            $pre = $_GET[$pagevarname] ? intval($_GET[$pagevarname])-1 : 1;
            $ret .= '<a href="javascript:'.$function_name.'(\''.$phpSelf.'\',\''.$pagevarname.'='.$pre.'\')">上一页</a>';
        } else {
            $ret .= '<span style="color:#aaaaaa">上一页</span>';
        }
        $ret .= ' - ';
        if ($this->hasNextPage()) {
            $next = $_GET[$pagevarname] ? intval($_GET[$pagevarname])+1 : 2;
            $ret .= '<a href="javascript:'.$function_name.'(\''.$phpSelf.'\',\''.$pagevarname.'='.$next.'\')">下一页</a>';
        } else {
            $ret .= '<span style="color:#aaaaaa">下一页</span>';
        }
        return $ret;
    }
}
?>
